//
//  ByViewShopTopPartSort.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/31.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewShopProductSort.h"

@implementation ByViewShopProductSort
{
    UIButton    *_defaultSortButton;
    UIButton    *_salesSortButton;
    UIControl   *_priceSortView;
    
    BOOL        _isPriceSorting;   // 是否是价格排序
    BOOL        _isAscend;         // 是否是升序
}
- (instancetype)init {
    
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _defaultSortButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _defaultSortButton.titleLabel.font = FONT(14);
    [_defaultSortButton setTitle:@"默认排序" forState:UIControlStateNormal];
    [_defaultSortButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_defaultSortButton setTitleColor:BY_GOLD_COLOR forState:UIControlStateSelected];
    [_defaultSortButton addTarget:self action:@selector(defaultSort) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_defaultSortButton];
    
    _salesSortButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _salesSortButton.titleLabel.font = FONT(14);
    [_salesSortButton setTitle:@"按销量排序" forState:UIControlStateNormal];
    [_salesSortButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_salesSortButton setTitleColor:BY_GOLD_COLOR forState:UIControlStateSelected];
    [_salesSortButton addTarget:self action:@selector(salesSort) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_salesSortButton];
    
    _priceSortView = [UIControl new];
    [_priceSortView addTarget:self action:@selector(priceSort) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_priceSortView];
    UIButton *priceSortButton = [UIButton buttonWithType:UIButtonTypeCustom];
    priceSortButton.userInteractionEnabled = false;
    priceSortButton.titleLabel.font = FONT(14);
    [priceSortButton setTitle:@"按价格排序" forState:UIControlStateNormal];
    [priceSortButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [priceSortButton setTitleColor:BY_GOLD_COLOR forState:UIControlStateSelected];
    UIButton *priceSortByAscendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    priceSortByAscendButton.userInteractionEnabled = false;
    [priceSortByAscendButton setImage:[UIImage imageNamed:@"升序-未选中"] forState:UIControlStateNormal];
    [priceSortByAscendButton setImage:[UIImage imageNamed:@"升序-选中"] forState:UIControlStateSelected];
    UIButton *priceSortByDescendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    priceSortByDescendButton.userInteractionEnabled = false;
    [priceSortByDescendButton setImage:[UIImage imageNamed:@"降序-未选中"] forState:UIControlStateNormal];
    [priceSortByDescendButton setImage:[UIImage imageNamed:@"降序-选中"] forState:UIControlStateSelected];

    _defaultSortButton.selected = true;
    priceSortButton.tag = 1;
    priceSortByAscendButton.tag = 2;
    priceSortByDescendButton.tag = 3;
    [_priceSortView addSubview:priceSortButton];
    [_priceSortView addSubview:priceSortByAscendButton];
    [_priceSortView addSubview:priceSortByDescendButton];
    
    
    
    // 布局
    [_defaultSortButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(@0);
        make.width.equalTo(self).multipliedBy(1.0 / 3);
    }];
    [_salesSortButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.width.equalTo(_defaultSortButton);
        make.left.equalTo(_defaultSortButton.mas_right);
    }];
    [_priceSortView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.width.equalTo(_salesSortButton);
        make.left.equalTo(_salesSortButton.mas_right);
    }];
    [priceSortButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(@0);
        make.centerX.equalTo(_priceSortView).offset(-10);
    }];
    [priceSortByAscendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(priceSortButton.mas_right).offset(2);
        make.size.mas_equalTo(CGSizeMake(10, 10));
        make.bottom.equalTo(_priceSortView.mas_centerY).offset(0);
    }];
    [priceSortByDescendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.left.equalTo(priceSortByAscendButton);
        make.top.equalTo(_priceSortView.mas_centerY).offset(0);
    }];
}


- (void)defaultSort {
    
    _defaultSortButton.selected = true;
    _salesSortButton.selected = false;
    
    _isPriceSorting = false;
    [self setPriceSortViewSelected];
}
- (void)salesSort {
    
    _defaultSortButton.selected = false;
    _salesSortButton.selected = true;
    
    _isPriceSorting = false;
    [self setPriceSortViewSelected];
}
- (void)priceSort {
    
    _defaultSortButton.selected = false;
    _salesSortButton.selected = false;
    
    // 当前正在价格搜索，则转变排序
    if (_isPriceSorting) {
        _isAscend = !_isAscend;
    } else {
        _isPriceSorting = true;
    }
    [self setPriceSortViewSelected];
}
- (void)setPriceSortViewSelected {
    
    _priceSortView.selected = _isPriceSorting;
    
    UIButton *textButton = [_priceSortView viewWithTag:1];
    UIButton *ascendButton = [_priceSortView viewWithTag:2];
    UIButton *descendButton = [_priceSortView viewWithTag:3];
    
    textButton.selected = _isPriceSorting;
    
    ascendButton.selected = _isAscend & _isPriceSorting;
    descendButton.selected = !_isAscend & _isPriceSorting;
}

@end
